package com.yjc.blog.utils;

import cn.hutool.core.bean.BeanUtil;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class CopyUtil {
    /**
     * 单体复制
     */

    public static <T> T copy(Object resource,Class<T> tClass){
        if (resource == null){
            return null;
        }
        T obj = null;
        try {
            obj = tClass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        BeanUtil.copyProperties(resource,obj);
        return obj;
    }

    /**
     * 多个复制
     */
    public static <T> List<T> copyList(List source,Class<T> tClass){
        List<T> target = new ArrayList<>();
        if (!CollectionUtils.isEmpty(source)){
            for (Object object : source) {
                T obj = copy(object,tClass);
                target.add(obj);
            }
        }
        return target;
    }

}
